package com.wondersgroup.fjzlpt.org.service.impl.jgqy.qylb;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.LoggerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qylb.QylbDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqKsxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqYljgDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.qylb.QylbService;


@Service
public class QylbServiceImpl implements QylbService{

	/*
	
	
	@Autowired
	private QysqYljgDAO qysqYljgDAO;
	
	@Autowired
	private YljgxxDAO yljgxxDao;
	
	*/
	
	@Autowired
	private QylbDAO qylbDAO;
	
	@Autowired
	private QysqYljgDAO qysqYljgDAO;
	
	@Autowired
	private QysqKsxxDAO qysqKsxxDAO;
	
	
	/**
	 * 保存
	 * @param map
	 * @return
	 */
	public JSONObject save(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		String method = map.remove("method");
		if("js".equals(method)){
			js(map, ro);
		}
		else if("jy".equals(method)){
			jy(map, ro);
		}
		else if("jysh".equals(method)){
			jysh(map, ro);
		}
		else if("xy".equals(method)){
			xy(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	
	/**
	 * 接收
	 * @param map
	 * @param ro
	 */
	private void js(Map map, ResultObject ro){
		try {
			map.put("QYJSSJ", new Date());
			JSONObject jo = qysqYljgDAO.query(map);
			map.put("YLJGID", jo.getString("YLJGID"));
			map.put("YLTID", jo.getString("YLTID"));
			qysqYljgDAO.update(map);
			map.put("JLZT", "1");
			qysqYljgDAO.updateForPrimaryKey(map);
			qylbDAO.updateForPrimaryKey(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000531","医联体接收申请失败！");
		}
	}

	/**
	 * 解约
	 * @param map
	 * @param ro
	 */
	private void jy(Map map, ResultObject ro){
		try {
			map.put("FQJYSJ", new Date());
			qysqYljgDAO.updateForPrimaryKey(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000532","医联体解约失败！");
		}
	}
	
	/**
	 * 解约审核
	 * @param map
	 * @param ro
	 */
	private void jysh(Map map, ResultObject ro){
		try {
			map.put("JYSHSJ", new Date());
			qysqYljgDAO.updateForPrimaryKey(map);
			JSONObject jo = qysqYljgDAO.query(map);
			map.put("YLJGID", jo.getString("YLJGID"));
			map.put("YLTID", jo.getString("YLTID"));
			qysqYljgDAO.update(map);
			if(qylbDAO.isExit(map)){
				map.put("YLTZT", "1");
				qylbDAO.updateForPrimaryKey(map);
			}else{
				map.put("YLTZT", "0");
				qylbDAO.updateForPrimaryKey(map);
			}
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000533","同意解约失败！");
		}
	}
	
	/**
	 * 续约
	 * @param map
	 * @param ro
	 */
	private void xy(Map<String, String> map, ResultObject ro){
		try {
			map.put("SFXY", "1");
			String ZDYLJGID = map.remove("ZDYLJGID");
			qylbDAO.updateForPrimaryKey(map);
			/**创建关联表插入的多条记录*/
			String yljgids = map.get("YLJGIDS");
			String YLTID = map.get("YLTID");
			String[] ids = yljgids.split(",");
			List<Map> list = new ArrayList<Map>();
			for (String str : ids) {
				Map  ma = new HashMap();
				ma.put("GLID", UUID.randomUUID().toString().toUpperCase());
				ma.put("YLTID", YLTID);
				ma.put("QYYXQ", map.get("QYYXQ"));
				ma.put("QYSJ", map.get("QYSJ"));
				ma.put("DQSJ", map.get("DQSJ"));
				ma.put("JLZT", "2");
				ma.put("YLJGID", str);
				if((ZDYLJGID).equals(str)){
					ma.put("JGLX", "3");
				}else{
					ma.put("JGLX", "1");
				}
				ma.put("QYSQSJ", (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
				ma.put("QYZT", "03");
				ma.put("JLZT", "2");
				list.add(ma);
			}
			qysqYljgDAO.insertBatch(list);
			/**判断是否为专科联盟*/
			if("03".equals(map.get("YLTLX"))){
				List<Map> ksList = new ArrayList<Map>();
				for (Map<String, String> ma : list) {
					String yljgid = ma.get("YLJGID");
					/**获取传入的多个ksdm*/
					String ksdms = map.get("KSDMS");
					ma.remove("QYZT");
					ids = ksdms.split("/");
					for (String str : ids) {
						if(str.indexOf(yljgid) != -1){
							String[] strAry = str.split(":");
							if("null".equals(strAry[1])){
								ma.put("KSDM", "null");
								ksList.add(ma);
							}else{
								System.out.println(ma.toString());
								String[] ksAry = strAry[1].split(",");
								for (int i = 0; i < ksAry.length; i++) {
									Map<String, String> mm = new HashMap<String, String>();
									mm.putAll(ma);
									mm.put("KSDM", ksAry[i]);
									ksList.add(mm);
								}
							}
						}
					}
				}
				/**插入专科联盟科室信息*/
				qysqKsxxDAO.insertBatch(ksList);
			}
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000534","同意解约失败！");
		}
	}
	
	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	//@Cacheable("queryPage")
	public JSONObject queryPage(Map<String, String> map) {
		return qylbDAO.queryPage(map);
	}

	/**
	 * 根据条件查询所有
	 * @param map
	 * @return
	 */
	public JSONArray queryList(Map<String, String> map) {
		JSONArray jsary = new JSONArray();
		try {
			jsary = qylbDAO.queryAll(map);
			JSONObject json = new JSONObject();
			json.put("YLTMC", "医联体名称");
			json.put("ZDYLJGMC", "主导机构");
			json.put("YLJGMC", "签约医疗机构");
			json.put("YLTLXMC", "医联体类型");
			json.put("QYSJ", "申请时间");
			json.put("QYYXQMC", "签约有效期");
			json.put("QYZTMC", "状态");
			jsary.add(0,json);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000535","数据查询失败");
		}
		return jsary;
	}


	/**
	 * 查询历史签约记录
	 * @param map
	 * @return
	 */
	public JSONObject queryLsDetail(Map<String, String> map) {
		return qylbDAO.queryPageLs(map);
	}

}
